<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {

    return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');


Route::get('/test',function (){
    $status=[
        'order_id'=>1,
        'user_id'=>1,
        'shipping'=>'已发货'
    ];
    //分发事件，并进行广播
    event(new \App\Events\ShippingStatusUpdated($status));

    return 'done';
});

Route::get('tasks',function () {
    return \App\Task::latest()->pluck('body');
});

Route::post('tasks',function (){
    $task = \App\Task::forceCreate(request(['body']));
    event(new \App\Events\TaskCreated($task));
});




Route::group(['middleware' => ['web','auth']], function () {
    //
    Route::get('projects/{project}',function (\App\Project $project) {
        $project->load('tasks');
        return view('projects.show',compact('project'));
    });

    Route::get('/api/projects/{project}',function (\App\Project $project) {
        return $project->tasks->pluck('body');
    });

    Route::post('/api/projects/{project}/tasks',function (\App\Project $project) {
        $task=$project->tasks()->create(request(['body']));
        event(new \App\Events\TaskCreated($task));
        return $task;
    });
});


